﻿using IPR_StudyMeetingMrgSys.Models;
using IPR_StudyMeetingMrgSys.BLL;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Reflection;
using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using System.Data;
using IPR_StudyMeetingMrgSys.ViewModel;
using Newtonsoft.Json;
using System.Text;
using System.Web;
using System.IO;
using IPR_StudyMeetingMrgSys.Common;

namespace IPR_StudyMeetingMrgSys.Controllers
{
    public class AttendanceInfoController : ApiController
    {
        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        /// <summary>
        /// 按月份、人员ID 汇总统计出勤信息
        /// </summary>
        /// <param name="swhere">查询条件</param>
        /// <param name="page">页索引</param>
        /// <param name="limit">页大小</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/AttendanceInfo/AttendanceInfoSummary")]
        public IHttpActionResult AttendanceInfoSummary(string swhere, int page, int limit)
        {
            log.Info(JsonConvert.SerializeObject(swhere + "#1"));
            ResultMsg resultMsg = new ResultMsg();
            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
            try
            {
                int startindex = (page - 1) * limit + 1;
                int endindex = page * limit;

                if (swhere != "")
                {
                    swhere = " 1=1 " + swhere;
                }
                else
                { swhere = " 1=1 "; }
                int rowCount = 0;
                bool blExist = false;
                DataSet dsSearch = new T_Attendance().GetListByPage(swhere, "UserID", startindex, endindex, out rowCount);
                if (dsSearch != null)
                    if (dsSearch.Tables.Count > 0)
                        if (dsSearch.Tables[0].Rows.Count > 0)
                        {
                            resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                            resultMsg.Count = rowCount;
                            resultMsg.Data = dsSearch.Tables[0];
                            blExist = true;
                        }
                if (!blExist)
                {
                    resultMsg.Info = "未获取到相应数据";
                }
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }



        [HttpGet]
        [Route("api/AttendanceInfo/GetProjectQuestionDetail")]
        public IHttpActionResult GetProjectQuestionDetail(string AttendanceDateTime, string UserID)
        {
            ResultMsg resultMsg = new ResultMsg();
            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
            try
            {
                DataTable dt = new BLL.T_Attendance().GetList(" T1.UserID='" + UserID+ "' and AttendanceDateTime like '"+ AttendanceDateTime + "%'").Tables[0];
                resultMsg.Info = dt.Rows.Count > 0 ? StatusCodeEnum.Success.GetEnumText() : "未获取到相应数据";
                resultMsg.Count = dt.Rows.Count;
                resultMsg.Data = dt;
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }
        
    }
}
